"""
Case Type   : SQL_LIMIT
Case Name   : 创建规则时，limit_type字段输入值为sqlid，创建成功
Create At   : 2025/9/16
Owner       : lonely-dance
Description :
    1、修改参数;
    2、执行任意sql并获取该sql的unique_sql_id
    3、创建限流limit，limit_type字段输入值为sqlid
    3、清理环境
Expect      :
    1、修改成功
    2、获取成功
    3、创建成功
    4、清理环境成功
History     :
"""

import os
import unittest
from yat.test import Node
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class SqlLimit(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----- {os.path.basename(__file__)} start-----')
        self.comm_sh = CommonSH('PrimaryDbUser')
        self.pri_node = Node('PrimaryDbUser')
        self.Constant = Constant()
        self.limit_name1 = 'limit0009_01'
        self.limit_id1 = 0

    def test_main(self):
        text = '----step1:修改参数 expect:成功----'
        self.log.info(text)
        self.comm_sh.execute_gsguc('reload', self.Constant.GSGUC_SUCCESS_MSG, 'enable_sql_limit = on')
        self.comm_sh.execute_gsguc('reload', self.Constant.GSGUC_SUCCESS_MSG, 'instr_unique_sql_count = 1000')

        text = '----step2:执行sql并获取该sql的unique_sql_id expect:获取成功----'
        self.log.info(text)
        cmd = f'''
        drop table if exists sql_limit0009;
        create table sql_limit0009 (id int, name varchar);
        insert into sql_limit0009 values(1,'a');
        '''
        self.log.info(cmd)
        res = self.comm_sh.execut_db_sql(cmd)
        self.log.info(res)
        self.assertNotIn('ERROR', res, '执行sql失败')

        cmd = f'''
        select unique_sql_id from dbe_perf.statement where query = 'insert into sql_limit0009 values(?,?);' 
        order by last_updated DESC limit 1;
        '''
        res = self.comm_sh.execut_db_sql(cmd)
        self.log.info(res)
        self.assertEqual('(1 row)', res.splitlines()[3].strip(), '获取unique_sql_id失败')
        unique_sql_id = int(res.splitlines()[-2].strip())

        text = '----step3:创建限流limit，limit_type字段输入值为sqlid expect：创建成功----'
        self.log.info(text)
        cmd = f'''
        select gs_create_sql_limit('{self.limit_name1}','sqlid',0,100,'now()','2025-3-30 23:59:59','{{{{{unique_sql_id}}}}}','','');
        '''
        self.log.info(cmd)
        res = self.comm_sh.execut_db_sql(cmd)
        self.log.info(res)
        self.assertEqual('(1 row)', str(res.splitlines()[-1].strip()), '创建限流limit失败')
        self.limit_id1 = int(res.splitlines()[-2].strip())

    def tearDown(self):
        text = '----step4:恢复环境 expect:成功----'
        self.log.info(text)
        cmd = f'select gs_delete_sql_limit({self.limit_id1});' \
              f'drop table if exists sql_limit0009;'
        res = self.comm_sh.execut_db_sql(cmd)
        self.log.info(res)
        self.comm_sh.execute_gsguc('reload', self.Constant.GSGUC_SUCCESS_MSG, 'enable_sql_limit = off')
        self.comm_sh.execute_gsguc('reload', self.Constant.GSGUC_SUCCESS_MSG, 'instr_unique_sql_count = 100')

        self.assertEqual(1, res.splitlines().count('(1 row)'), '执行失败' + text)
        self.assertIn('DROP TABLE', res, '执行失败' + text)
        self.log.info(f'----- {os.path.basename(__file__)} end-----')
